package org.ws.edu.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.ws.edu.common.model.Profile;

public class ProfileDao extends GenericDao<Profile> {

    @Override
    public Profile get(final Long id) {
        return hibernateTemplate.execute(new HibernateCallback<Profile>() {

            public Profile doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                criteria.add(Property.forName("id").eq(id));
                return (Profile) criteria.uniqueResult();
            }
        });
    }

    @Override
    public void delete(final Long id) {
        hibernateTemplate.execute(new HibernateCallback<Boolean>() {

            public Boolean doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                criteria.add(Property.forName("id").eq(id));
                if (criteria.uniqueResult() != null) {
                    session.delete(criteria.uniqueResult());
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    @Override
    public List<Profile> getList() {
        return hibernateTemplate.execute(new HibernateCallback<List<Profile>>() {

            public List<Profile> doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Profile.class);
                return criteria.list();
            }
        });
    }

//    public Map authorization(final String login, final String passwordMd5) {
//
//        Map<String, String> map = new HashMap<String, String>();
//        Long profileid = hibernateTemplate.execute(new HibernateCallback<Long>() {
//
//            public Long doInHibernate(Session session) throws HibernateException, SQLException {
//                Query query = session.createQuery("from Profile where name = :login AND passwordHash = :password");
//                query.setParameter("login", login);
//                query.setParameter("password", passwordMd5);
//                if (query.uniqueResult() == null) {
//                    return 0l;
//                } else {
//                    return ((Profile) query.uniqueResult()).getId();
//                }
//            }
//        });
//
//        if (profileid != 0) {
//            return hibernateTemplate.execute(new HibernateCallback<Map>() {
//
//                public Map doInHibernate(Session session) throws HibernateException, SQLException {
//
//                    Map<String, String> map = new HashMap<String, String>();
//                    map.put("token", token);
//                    map.put("authorization", "success");
//                    session.createSQLQuery("INSERT INTO profile_hash (profile_id, authorization_hash) VALUES ");
//
//                    return map;
//                }
//            });
//        } else {
//            map.put("authorization", "fail");
//            return map;
//
//        }
//    }
}
